/*
 * @Description  : 
 * @Author       : CaiJiZhou
 * @Date         : 2021-12-14 11:12:07
 * @LastEditors  : Shaquille.Cai
 * @LastEditTime : 2021-12-16 15:16:21
 * @FilePath     : \\vue3-admin-with-blog\\src\\plugins\\permission.js
 * Copyright (C) 2021 Shaquille.Cai. All rights reserved.
 */
import mainRoutes from "@/router/mainRoutes";

// https://www.cnblogs.com/ljx20180807/p/14665505.html
const filterAsyncRouter = (asyncRouterMap) => {
    const modules = getModules();
    const components = getComponents();
    const allFileList=[]
    Object.keys(modules).forEach(key => {
        const viewSrc = components[key];
        const file = viewSrc.default;
        allFileList.push({
            name: `${file.name}`,
            component: modules[key]
          })
    })
    const accessedRouters = [...asyncRouterMap]
    allFileList.forEach(v => {
        accessedRouters.forEach(route => {
            if (v.name===route.name) {
                route.component=v.component
            }
        })
    })
    return accessedRouters
}
function getModules() {
    const components = import.meta.glob('../views/**/*.vue')
    return components
  }
  function getComponents() {
    const components = import.meta.globEager('../views/**/*.vue')
    return components
  }


export default {
    install: (app, { router, store }) => {
        
        router.beforeEach(async (to, from, next) => {
                document.title = to.meta.title || '';
                const hasToken = !!store.getters['moduleInfo/token']
                if (hasToken) {
                    if (to.path === '/login') {
                        next({
                            path: '/welcome'
                        })
                    } else {
                        const hasGetUserInfo = store.getters['moduleInfo/userInfo'].userName
                        if (hasGetUserInfo && router.options.isAddDynamicMenuRoutes) {
                            next()
                        } else {
                            try {
                                console.info('有token并且去的不是登录页,但是没有用户信息亦或是初始加载~')
                                await store.dispatch('moduleInfo/getUserInfo')
                                const menuResult = await store.dispatch('moduleInfo/setMenuRes')
                                const data = filterAsyncRouter(menuResult)
                                mainRoutes['children']=[...mainRoutes['children'],...data]
                                router.addRoute(mainRoutes)
                                router.options.isAddDynamicMenuRoutes=true
                                next({
                                    ...to,
                                    replace: true
                                })
                            } catch (error) {
                                await store.dispatch('moduleInfo/resetToken') //触发  resetToken
                                next()
                            }
                        }
                    }
                } else if(!hasToken && to.path === '/login'){
                    next()
                }else if (!hasToken && to.path !== '/login') {
                    next({
                        path: '/login'
                    })
                }
            
        })
    }
}